package com.hainatong.jcmvvmbase.mainhome;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.jcmvvmbase.Constants;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.ActivityCommonTitleBinding;
import com.hainatong.jcmvvmbase.login.fragment.*;
import com.hainatong.jcmvvmbase.mainhome.fragment.*;
import com.hainatong.jcmvvmbase.mainhome.fragment.holiday.FireWorkerFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.holiday.HolidayApplyResultFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.holiday.MyHolidayFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.holiday.NormalHolidayFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.massage.ApprovalMsgFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.massage.SysMsgFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.massage.WarnMsgDetailFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.massage.WarnMsgFragment;
import com.hainatong.jcmvvmbase.mainhome.fragment.warehouse.*;
import com.hainatong.mvvmbase.base.BaseMvvmActivity;
import com.hainatong.mvvmbase.base.NoViewModel;

import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.appcompat.widget.Toolbar;
import com.hainatong.mvvmbase.rxbus.RxBus;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;
import io.reactivex.disposables.Disposable;


public class ComFragmentActivity extends BaseMvvmActivity<ActivityCommonTitleBinding, NoViewModel>
{
    private FragmentTransaction ft;

    public static void start(Activity mContext, Bundle bundle)
    {
        start(mContext, bundle, false);
    }

    public static void start(Activity mContext, Bundle bundle, boolean isFinish)
    {
        Intent intent = new Intent();
        intent.setClass(mContext, ComFragmentActivity.class);
        if (bundle != null) intent.putExtra(mContext.getPackageName(), bundle);
        mContext.startActivity(intent);
        if (isFinish)
        {
            mContext.finish();
        }
    }

    public Bundle getBundle()
    {
        if (getIntent() != null && getIntent().hasExtra(getPackageName()))
            return getIntent().getBundleExtra(getPackageName());
        else
            return null;
    }

    @Override
    protected int getLayoutId()
    {
        return R.layout.activity_common_title;
    }

    @Override
    protected int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void initView()
    {
        ft = getSupportFragmentManager().beginTransaction();

        Bundle bundle = getBundle();
        String type = bundle.getString(Constants.FRAGMNET_TYPE, "");

        String content = bundle.getString(Constants.SCAN_CONTENT_CODE, "");

        if (!StringUtils.isEmpty(type))
        {
            switch (type)
            {
                case Constants.USER_INFO:
                    openUserInfo(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.SET_PWD:
                    openChangePwd(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.HAND_PWD:
                    openHandlePassword(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.CHANGE_LANGUAGE:
                    openLanguageSelected(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.SIGN_NAME:
                    ToastUtils.showShort("登出");
                    break;
                // 功能列表

                case Constants.FUN_STOCK_OUT_IN_MGR:
                    openWareHouseMgr(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.FUN_HOLIDAY_MGR:
                    openHolidayMgr(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.FUN_WORK_MGR:
                    openWorkMgr(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.FUN_QSHE_MGR:
                    openQSHEMgr(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                // 仓库管理
                // 入库
                case Constants.VIEW_TYPE_RUKU:
                    if (!StringUtils.isEmpty(content))
                    {
                        openShelf(viewDataBinding.includeTitle.toolbar, ft, content);
                    }
                    break;
                case Constants.VIEW_TYPE_RUKU_TASK:
                    if (!StringUtils.isEmpty(content))
                    {
                        openRukuTask(viewDataBinding.includeTitle.toolbar, ft, content);
                    }
                    break;
                case Constants.VIEW_TYPE_CHUKU:
                    if (!StringUtils.isEmpty(content))
                    {
                        openChuKu(viewDataBinding.includeTitle.toolbar, ft, content);
                    }
                    break;
                case Constants.VIEW_TYPE_RETURN:
                    if (!StringUtils.isEmpty(content))
                    {
                        openReturnAdd(viewDataBinding.includeTitle.toolbar, ft, content);
                    }
                    break;
                case Constants.VIEW_TYPE_BROKEN_SUB:
                    if (!StringUtils.isEmpty(content))
                    {
                        openBrokenSub(viewDataBinding.includeTitle.toolbar, ft, content);
                    }
                    break;
                case Constants.VIEW_TYPE_RUKU_ADD:
                    if (!StringUtils.isEmpty(content))
                    {
                        openRukuAdd(viewDataBinding.includeTitle.toolbar, ft, content);
                    }
                    break;

                case Constants.VIEW_TYPE_LINGYONG_INFO:
                    openLinYongInfo(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.VIEW_TYPE_LINGYONG:
                    openLinYongRecord(viewDataBinding.includeTitle.toolbar, ft);
                    break;

                case Constants.VIEW_SYS_MSG:
                    openSysMsg(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.VIEW_WARN_MSG:
                    openWarnMsg(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.VIEW_APRV_MSG:
                    openApprovalMsg(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.VIEW_WARN_MSG_DEATIL:
                    openWarnMsgDetail(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.VIEW_NORMAL_HOLIDAY:
                    if (!StringUtils.isEmpty(content))
                    {
                        openNormalHoliday(viewDataBinding.includeTitle.toolbar, ft,content);
                    }
                    break;
                case Constants.VIEW_NO_JOB:
                    openFireWorker(viewDataBinding.includeTitle.toolbar, ft);
                    break;
                case Constants.VIEW_HOLIDAY_APPLY_DETAIL:
                    openApplyDetail(viewDataBinding.includeTitle.toolbar, ft);
                    break;

            }
        }

        initRxBus();
    }

    private void initRxBus()
    {
        Disposable disposable = RxBus.getDefault().toObservable(Constants.PWD_SUCCESS_CODE,
            RxBusBaseMessage.class).subscribe(result -> {
                showResult(result.getCode());
            });
        addSubscription(disposable);
    }

    /**
     * 显示 修改密码 结果
     * 
     * @param pwdType
     */
    private void showResult(int pwdType)
    {
        ResultFragment resultFragment = new ResultFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.PWD_SUCCESS_TYPE, pwdType);
        resultFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
            resultFragment).commit();
    }

    @Override
    protected void initData()
    {}

    @Override
    protected void onRetryBtnClick()
    {

    }

    private void openUserInfo(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.m_user_info));
        setToolBar(toolbar);
        openFragment(mFt, new UserInfoFragment());
    }

    private void openChangePwd(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.change_pwd));
        setToolBar(toolbar);
        openFragment(mFt, new ChangePwdFragment());
    }

    private void openLanguageSelected(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.language_tag));
        setToolBar(toolbar);
        openFragment(mFt, new LanguageFragment());
    }

    private void openHandlePassword(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.set_handle_pwd));
        setToolBar(toolbar);
        openFragment(mFt, new HandlePwdFragment());
    }

    private void openFragment(FragmentTransaction mFt, Fragment fragment)
    {
        mFt.add(R.id.fragment_container, fragment).commit();
    }

    private void setToolbarTitle(Toolbar toolbar, String title)
    {
        // toolbar.setBackgroundColor(ContextCompat.getColor(this,R.color.red));

        ((TextView)toolbar.findViewById(R.id.tv_title)).setText(title);
        ((ImageButton)toolbar.findViewById(R.id.btn_back)).setImageResource(R.drawable.svg_back);
        toolbar.findViewById(R.id.btn_back).setOnClickListener(v -> {
            supportFinishAfterTransition();
        });
    }

    private void setToolbarSubButton(Toolbar toolbar, int resId, View.OnClickListener listener)
    {
        ((ImageButton)toolbar.findViewById(R.id.btn_sub)).setImageResource(resId);
        toolbar.findViewById(R.id.btn_sub).setOnClickListener(listener);
    }

    // 功能列表
    private void openWareHouseMgr(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.stock_out_in_mgr));
        setToolBar(toolbar);
        openFragment(mFt, new WareHouseMgrFragment());
    }

    private void openHolidayMgr(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.xiujia_mgr));
        setToolBar(toolbar);
        openFragment(mFt, new MyHolidayFragment());
    }

    private void openWorkMgr(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.work_mgr));
        setToolBar(toolbar);
        openFragment(mFt, new WorkMgrFragment());
    }

    private void openQSHEMgr(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.qshe_mgr));
        setToolBar(toolbar);
        openFragment(mFt, new QSHEMgrFragment());
    }

    // 仓库管理
    // 入库
    private void openShelf(Toolbar toolbar, FragmentTransaction mFt, String scanCode)
    {
        setToolbarTitle(toolbar, getString(R.string.fun_ruku));
        setToolBar(toolbar);
        openFragment(mFt, ShelfFragment.newInstance(scanCode));
    }

    private void openRukuTask(Toolbar toolbar, FragmentTransaction mFt, String scanCode)
    {
        setToolbarTitle(toolbar, getString(R.string.fun_ruku));
        setToolBar(toolbar);
        openFragment(mFt, RukuTaskFragment.newInstance(scanCode));
    }

    private void openChuKu(Toolbar toolbar, FragmentTransaction mFt, String scanCode)
    {
        setToolbarTitle(toolbar, getString(R.string.fun_chuku));
        setToolBar(toolbar);
        openFragment(mFt, ChukuAddFragment.newInstance(scanCode));
    }

    private void openReturnAdd(Toolbar toolbar, FragmentTransaction mFt, String scanCode)
    {
        setToolbarTitle(toolbar, getString(R.string.fun_return));
        setToolBar(toolbar);
        openFragment(mFt, ReturnAddFragment.newInstance(scanCode));
    }

    private void openBrokenSub(Toolbar toolbar, FragmentTransaction mFt, String scanCode)
    {
        setToolbarTitle(toolbar, getString(R.string.broken_submit));
        setToolBar(toolbar);
        openFragment(mFt, BrokenSubmitFragment.newInstance(scanCode));
    }

    private void openRukuAdd(Toolbar toolbar, FragmentTransaction mFt, String scanCode)
    {
        setToolbarTitle(toolbar, getString(R.string.fun_ruku));
        setToolBar(toolbar);
        openFragment(mFt, RukuAddFragment.newInstance(scanCode));
    }

    private void openLinYongInfo(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.select_lingyong_worker));
        setToolBar(toolbar);
        openFragment(mFt, new LinYongFragment());
    }

    private void openLinYongRecord(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.fun_linyongjilu));
        setToolBar(toolbar);
        openFragment(mFt, new LinYongRecordFragment());
    }

    private void openSysMsg(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.sys_msg));
        setToolBar(toolbar);
        openFragment(mFt, new SysMsgFragment());
    }

    private void openWarnMsg(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.warn_msg));
        setToolBar(toolbar);
        openFragment(mFt, new WarnMsgFragment());
    }

    private void openApprovalMsg(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.examine_msg));
        setToolBar(toolbar);
        openFragment(mFt, new ApprovalMsgFragment());
    }

    private void openWarnMsgDetail(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.stock_warn));
        setToolBar(toolbar);
        openFragment(mFt, new WarnMsgDetailFragment());
    }

    private void openNormalHoliday(Toolbar toolbar, FragmentTransaction mFt , String scanCode)
    {
        setToolbarTitle(toolbar, getString(R.string.zhengchang_xiujia));
        setToolBar(toolbar);
        openFragment(mFt, NormalHolidayFragment.newInstance(scanCode));
    }

    private void openFireWorker(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.can_not_job));
        setToolBar(toolbar);
        openFragment(mFt, new FireWorkerFragment());
    }



    private void openApplyDetail(Toolbar toolbar, FragmentTransaction mFt)
    {
        setToolbarTitle(toolbar, getString(R.string.xiujia_mgr));
        setToolBar(toolbar);
        openFragment(mFt, new HolidayApplyResultFragment());
    }




}
